/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.client.member.logic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import mscs.data.pojo.GiftRecord;
import mscs.data.pojo.Member;

/**
 *
 * @author Administrator
 */
public class GiftUtil implements IGiftUtil{

    public List<Member> getGiftMembers(List<Member> memberList, List<GiftRecord> giftRecordList, List<Integer> reasonList) {
        List<Member> giftMembers = new ArrayList<Member>();
        for(Member member: memberList){
            if(this.isBirthdayReason(member, giftRecordList)){
                giftMembers.add(member);
                reasonList.add(0);
            }
            if(this.isPointsReason(member, giftRecordList)!=0){
                giftMembers.add(member);
                reasonList.add(this.isPointsReason(member, giftRecordList));
            }
        }
        return giftMembers;
    }
    
    private  boolean isBirthdayReason(Member member, List<GiftRecord> giftRecordList){
        boolean flag = false;
        Calendar rightnow = Calendar.getInstance();
        Calendar birthday = Calendar.getInstance();
        birthday.setTime(member.getBirthday());
        birthday.set(Calendar.YEAR, rightnow.get(Calendar.YEAR));
        if(birthday.before(rightnow)){
            birthday.add(Calendar.DATE, 3);
            if(birthday.after(rightnow)){
                flag = true;
            }
        }else{
            rightnow.add(Calendar.DATE, 3);
            if(rightnow.after(birthday)){
                flag = true;
            }
        }
        if(flag == true){
            for(GiftRecord record: giftRecordList){
                if(record.getMemberID().equals(member.getId()) && record.getReason()==0){
                    return false;
                }
            }
            return true;
        }
        return false;
    }
    
    private int isPointsReason(Member member, List<GiftRecord> giftRecordList){
        int maxPoint = 0;
        for(int i=0; i<giftRecordList.size(); i++){
            if(giftRecordList.get(i).getMemberID().equals(member.getId()) && giftRecordList.get(i).getReason()>=maxPoint){
                maxPoint = giftRecordList.get(i).getReason();
            }
        }
        int newLimit = 0;
        switch(maxPoint){
            case 0: newLimit = 1000;break;
            case 1000: newLimit = 2000;break;
            case 2000: newLimit = 5000;break;
            default: newLimit = maxPoint + 5000;break;
        }
        if(member.getTotalPoints()>=newLimit){
            return newLimit;
        }else{
            return 0;
        }
    }
    
}
